<?php
class EditAction extends Action {
	public function index() {
	}

	public function insert() {
		header("Content-Type:text/html; charset=utf-8");
		$homework = D("homeworkinfo");

        $homeworkinfo_dao = M('homeworkinfo');
		$data['head'] = $_POST['head'];
		$data['content'] = $_POST['content'];
		$end_time = $_POST['deadline'];
		$year = substr($end_time, 0, 4);
		$month = substr($end_time, 5, 2);
		$day = substr($end_time, 8, 2);
		$end_time = gmmktime(0, 0, 0, $month, $day, $year);
		$data['deadline'] = $end_time;
		$last_id = $homeworkinfo_dao->add($data);
		$this->ajaxReturn($last_id, $last_id , 1);
	}

	public function editHomework() {
		header("Content-Type:text/html; charset=utf-8");
		$condition['id'] = $_GET['hw_id'];
		$homework = M('homeworkinfo') -> where($condition) -> find();
		$this -> id = $homework[id];
		$this -> head = $homework['head'];
		$this -> content = $homework['content'];
		$this -> deadline = date('Y-m-d', $homework['deadline']);
		$this -> display();
	}

	public function update() {
		//header("Content-Type:text/html; charset=utf-8");
		//$Dao = D("homeworkinfo");
		//$data = $Dao -> create();
		
		$hw_id = $_POST['id'];
		$end_time = $_POST['deadline'];
		$year = substr($end_time, 0, 4);
		$month = substr($end_time, 5, 2);
		$day = substr($end_time, 8, 2);
		$end_time = gmmktime(0, 0, 0, $month, $day, $year);
		$data['head'] = $_POST['head'];
		$data['content'] = $_POST['content'];
		$data['deadline'] = $end_time;
		$homeworkinfo_dao = M('homeworkinfo');
		$condition;
		$condition['id'] = $hw_id;
		if ($homeworkinfo_dao -> where($condition) -> save($data)) {
			//echo '作业更新成功！';
			$this->ajaxReturn(null, null , 1);
			
		} else {
			//echo $Dao -> getError();
			$this->ajaxReturn(null, null, 0);
		}

	}
	
	public function delete() {
		
		$Dao = M('homeworkinfo');
		$condition['id'] = $_GET['hw_id'];
		$result = $Dao->where($condition)->delete();
		if(result == true){
			$this->ajaxReturn(null, null, 1);
		}
		else {
			$this->ajaxReturn(null, null, 0);
		}

	}

}
?>